$(function() {
    $('form').parsley();
    dynamicBinding();
    getAllResExperts();
});
//遍历所有resExperts输出到表格
function getAllResExperts() {
    let deleteStr = "";
    let updateStr = "";
    let columns = [
        {title: "序号", data: null,"render": function (data,type,row,meta) {
                return data=Number(meta.row)+Number(1);
            }},
        {title: "姓名", data: "name"},
        {title: "性别", data: "sex"},
        {title: "出生日期", data: "birth"},
        {title: "专家类别", data: "category"},
        {title: "专家类型", data: "type"},
        {title: "适用事件类型", data: "eventType"},
        {title: "所属工作单位", data: "departmentId"},
        {title: "职称", data: "position"},
        {title: "联系电话", data: "tel"},
        {title: "操作", data: "id","orderable": false, // 禁用排序
            "defaultContent": "","render": function (data,type,row,meta) {
                if (hasDeletePermission) {
                    deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                }
                if (hasUpdatePermission) {
                    updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updateResExpertsModal'>更新</button>";
                }
                return data = deleteStr+updateStr;}}
    ];
    let url ='resExperts/getAllResExperts';
    dataTableInit(url,columns);
}
//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind( '/resExperts/deleteResExperts');
    //为所有的update按钮绑定update事件
    $("#datatable").on('click', ".data-update", function () {
        let data_id = $(this).attr('data-id');
        let row = $("#datatable").DataTable().row($(this).parents('tr')).data();
        $("#toUpdateResExperts"+" [name]").each(function () {
            let name =  $(this).attr("name");
            $(this).val(row[name]);
        });
        $('#toUpdateResExperts').parsley().on('form:validate', function () {
        }).on('form:submit', function () {
            let id = data_id;
            let data = new FormData($('#toUpdateResExperts')[0]);
            data.set('birth',Date($(" #updateBirth ").val()));
            data.set("id", id);
            updateAjax(data, '/resExperts/updateResExperts', false, false, "#updateResExpertsModal");
            return false;
        });
    });
    //为所有的add按钮绑定add事件
    $('#toAddResExperts').parsley().on('form:validate', function () {
    }).on('form:submit', function () {
        let data = new FormData($('#toAddResExperts')[0]);
        data.set('birth',Date($(" #addBirth ").val()));
        addAjax(data, '/resExperts/addResExperts', false, false, "#addResExpertsModal");
        return false;
    });

    //为add模态框绑定消失事件-清空模态框中内容
    $('#addResExpertsModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddResExperts").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updateResExpertsModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdateResExperts").reset();
    });
}
